package com.moxiao.yfjh.module.maker.service.matchMakerStringing;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.moxiao.yfjh.module.maker.controller.admin.matchMakerStringing.vo.MatchmakerStringingCreateReqVO;
import com.moxiao.yfjh.module.maker.controller.admin.matchMakerStringing.vo.MatchmakerStringingExportReqVO;
import com.moxiao.yfjh.module.maker.controller.admin.matchMakerStringing.vo.MatchmakerStringingPageReqVO;
import com.moxiao.yfjh.module.maker.controller.admin.matchMakerStringing.vo.MatchmakerStringingUpdateReqVO;
import com.moxiao.yfjh.module.maker.dal.dataobject.matchMakerStringing.MatchmakerStringingDO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;

import java.util.*;

import com.moxiao.yfjh.module.maker.controller.admin.matchMakerStringing.vo.*;
import com.moxiao.yfjh.framework.common.pojo.PageResult;

import com.moxiao.yfjh.module.maker.convert.matchMakerStringing.MatchmakerStringingConvert;
import com.moxiao.yfjh.module.maker.dal.mysql.matchMakerStringing.MatchmakerStringingMapper;

import static com.moxiao.yfjh.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.moxiao.yfjh.module.maker.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 相亲红娘牵线记录 Service 实现类
 *
 * @author mx
 */
@Service
@Validated
public class MatchmakerStringingServiceImpl implements MatchmakerStringingService {

    @Resource
    private MatchmakerStringingMapper matchmakerStringingMapper;

    @Override
    public Long createMatchmakerStringing(MatchmakerStringingCreateReqVO createReqVO) {
        // 插入
        MatchmakerStringingDO matchmakerStringing = MatchmakerStringingConvert.INSTANCE.convert(createReqVO);
        matchmakerStringingMapper.insert(matchmakerStringing);
        // 返回
        return matchmakerStringing.getId();
    }

    @Override
    public void updateMatchmakerStringing(MatchmakerStringingUpdateReqVO updateReqVO) {
        // 校验存在
        validateMatchmakerStringingExists(updateReqVO.getId());
        // 更新
        MatchmakerStringingDO updateObj = MatchmakerStringingConvert.INSTANCE.convert(updateReqVO);
        matchmakerStringingMapper.updateById(updateObj);
    }

    @Override
    public void deleteMatchmakerStringing(Long id) {
        // 校验存在
        validateMatchmakerStringingExists(id);
        // 删除
        matchmakerStringingMapper.deleteById(id);
    }

    private void validateMatchmakerStringingExists(Long id) {
        if (matchmakerStringingMapper.selectById(id) == null) {
            throw exception(MATCHMAKER_STRINGING_NOT_EXISTS);
        }
    }

    @Override
    public MatchmakerStringingDO getMatchmakerStringing(Long id) {
        return matchmakerStringingMapper.selectById(id);
    }

    @Override
    public List<MatchmakerStringingDO> getMatchmakerStringingList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return matchmakerStringingMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<MatchmakerStringingDO> getMatchmakerStringingPage(MatchmakerStringingPageReqVO pageReqVO) {
        return matchmakerStringingMapper.selectPage(pageReqVO);
    }

    @Override
    public List<MatchmakerStringingDO> getMatchmakerStringingList(MatchmakerStringingExportReqVO exportReqVO) {
        return matchmakerStringingMapper.selectList(exportReqVO);
    }

    @Override
    public MatchmakerStringingDO getLastNo(Date todayStart, Date todayEnd) {
        LambdaQueryWrapper<MatchmakerStringingDO> lqw = Wrappers.lambdaQuery();
        if (Objects.nonNull(todayStart) && Objects.nonNull(todayEnd)) {
            lqw.between(MatchmakerStringingDO::getCreateTime, todayStart, todayEnd);
        }
        lqw.orderByDesc(MatchmakerStringingDO::getCreateTime);
        lqw.last("LIMIT " + 1);
        return matchmakerStringingMapper.selectOne(lqw);
    }

    @Override
    public Integer repeatChecking(String code) {
        LambdaQueryWrapper<MatchmakerStringingDO> lqw = Wrappers.lambdaQuery();
        lqw.eq(true, MatchmakerStringingDO::getNo, code);
        List<MatchmakerStringingDO> list = matchmakerStringingMapper.selectList(lqw);
        return list.size();
    }

}
